package cn.yshujia.controller;

import cn.yshujia.annotation.DecryptFields;
import cn.yshujia.constant.SystemConst;
import cn.yshujia.domain.dto.PageDTO;
import cn.yshujia.domain.entity.User;
import cn.yshujia.domain.vo.ApiVO;
import cn.yshujia.domain.vo.PageVO;
import cn.yshujia.domain.vo.UserVO;
import cn.yshujia.ex.DefineExceptionHandler;
import cn.yshujia.service.impl.UserServiceImpl;
import com.alibaba.fastjson2.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 控制器
 */

@RestController
@Tag (name = "User", description = "用户Api")
public class UserController {
	
	@Resource
	private UserServiceImpl service;
	
	@GetMapping ("/user/info")
	@DecryptFields
	@Operation (summary = "根据 token 获取用户信息")
	public ApiVO<UserVO> info(HttpServletRequest request) {
		String token = request.getHeader(SystemConst.TOKEN_KEY);
		if (ObjectUtils.isEmpty(token)) {
			return ApiVO.error();
		}
		return ApiVO.success(service.userInfo(token));
	}
	
	@GetMapping ("/admin/user/page")
	@Operation (summary = "admin 分页查询用户")
	@PreAuthorize ("@sys.hasOnePermission('user:all')")
	public ApiVO<PageVO<UserVO>> page(@RequestParam String dto) {
		PageDTO pageDTO = JSON.parseObject(dto, PageDTO.class);
		if (null == pageDTO) {
			return ApiVO.error();
		}
		return ApiVO.success(service.adminPage(pageDTO));
	}
	
	@GetMapping ("/admin/user/distribution")
	@Operation (summary = "用户分布柱状图数据")
	public ApiVO<Map<String, List<String>>> selectCountGroupByRoleId() {
		return ApiVO.success(service.selectCountGroupByRoleId());
	}
	
	@PostMapping ("/user/insert")
	@DecryptFields (value = {"email", "password"})
	@Operation (summary = "注册用户")
	@PreAuthorize ("@sys.hasOnePermission('user:all', 'user:insert')")
	public ApiVO<Boolean> insert(@RequestParam String email, @RequestParam String password) throws DefineExceptionHandler {
		Boolean result = service.insert(email, password);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
	@PostMapping ("/admin/user/insert")
	@DecryptFields (value = {"password"})
	@Operation (summary = "admin 添加用户")
	@PreAuthorize ("@sys.hasOnePermission('user:all')")
	public ApiVO<Boolean> insert(@RequestBody User user) throws DefineExceptionHandler {
		Boolean result = service.insert(user);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
	@PutMapping ("/user/update")
	@DecryptFields (value = {"email", "password"})
	@Operation (summary = "修改用户信息")
	@PreAuthorize ("@sys.hasOnePermission({'user:all', 'user:update'})")
	public ApiVO<Boolean> update(@RequestBody User user) {
		Boolean result = service.update(user);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
}